package com.dq.controller.op.opboss.set;

import com.dq.exception.customException.AdminException;
import com.dq.controller.in.GiftGrantSetRecordQuery;
import com.dq.vo.Vo;
import com.dq.domain.org.Org;
import com.dq.domain.record.GiftGrantRecord;
import com.dq.domain.setup.GiftGrantSet;
import com.dq.domain.sys.user.user.base.SysUser;
import com.dq.domain.sys.user.user.WxUser;
import com.dq.service.AllService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author luchangtong
 * @Date 2019/12/30 15:11
 */
@Api(description = "后台礼物发放记录（员工/顾客）")
@RestController
@RequestMapping("/sys/boss/giftGrantRecord")
public class GiftGrantRecordController extends AllService {

    @ApiOperation("列表")
    @PostMapping("list")
    @RequiresPermissions("sys:giftGrantRecord:list")
    public Vo list(@RequestBody GiftGrantSetRecordQuery giftGrantSetRecordQuery)throws AdminException{
        return giftGrantSetRecordService.list(giftGrantSetRecordQuery);
    }

    @ApiOperation("添加")
    @PostMapping("add/{giftGrantSetId}")
    @RequiresPermissions("sys:giftGrantRecord:add")
    public Vo add(@PathVariable Long giftGrantSetId,@RequestBody GiftGrantRecord giftGrantRecord)throws AdminException{
        return giftGrantSetRecordService.add(giftGrantRecord,giftGrantSetId);
    }

    @ApiOperation("编辑")
    @PostMapping("edit/{id}/{giftGrantSetId}")
    @RequiresPermissions("sys:giftGrantRecord:edit")
    public Vo edit(@PathVariable Long id,@PathVariable Long giftGrantSetId,@RequestBody GiftGrantRecord giftGrantRecordEdit) throws AdminException{
        return giftGrantSetRecordService.edit(giftGrantRecordEdit,id,giftGrantSetId);
    }

    @ApiOperation("删除")
    @PostMapping("delete/{id}")
    @RequiresPermissions("sys:giftGrantRecord:delete")
    public Vo delete(@PathVariable Long id)throws AdminException{
        return giftGrantSetRecordService.delete(id);
    }

    @ApiOperation("根据门店查看发放类别")
    @GetMapping("findAllCategoryByOrg")
    public Vo findAllCategoryByOrg()throws AdminException{
        SysUser sysUser = shiroService.getUser();
        List<GiftGrantSet> list = giftGrantSetRepository.findByOrgAndDeletedFalse((Org) sysUser.getSection());
        List<Map> maps = new ArrayList<>();
        list.forEach(giftGrantSet -> {
            Map<String,Object> map = new HashMap<>();
            map.put("name",giftGrantSet.getCategoryName());
            map.put("id",giftGrantSet.getId());
            maps.add(map);
        });
        return new Vo(maps);
    }

    @ApiOperation("根据门店查下面员工")
    @GetMapping("findAllStaffByOrg")
    public Vo findAllStaffByOrg()throws AdminException{
        return communalService.findStaffByOrg();
    }

    @ApiOperation("根据门店查下面顾客")
    @GetMapping("findAllWxUserByOrg")
    public Vo findAllWxUserByOrg()throws AdminException{
        SysUser sysUser = shiroService.getUser();
        List<WxUser> list = wxUserRepository.findByDeletedAndSection(false,sysUser.getSection());
        List<Map> maps = new ArrayList<>();
        list.forEach(wxUser -> {
            Map<String,Object> map = new HashMap<>();
            map.put("id",wxUser.getId());
            map.put("name",wxUser.getUserRealcName());
            maps.add(map);
        });
        return new Vo(maps);
    }
}
